<?php
//代码替换
defined('BY_JYA') or exit('error');
global $_RQ,$_AR;
$_RQ['op']=$_RQ['op']?$_RQ['op']:'get';
$return=array();
$return['types']=array(
	'1'=>'.php',
	'2'=>'.html',
	'3'=>'.js'
);
if($_RQ['op']=='get'){
	$params=array(
		'table'=>'replace',
	);
	$return['replaces']=$this->get_list('replace');
	if($return['replaces']){
		foreach($return['replaces'] as &$rep){
			$rep['search']=base64_decode($rep['search']);
			$rep['replace']=base64_decode($rep['replace']);
		}
	}
	$params=array(
		'fields'=>array('id','title')
	);
	$return['plugins']=$this->get_list('plugin',$params);
	$return['item']=array();
	exi($return);
}
if($_RQ['op']=='post'){
	$post=get_postdata();
	//获取替换模块
	$plugin=intval($post['plugin']);
	$plugin=$this->getcolumn('plugin',array('id'=>$plugin),'name');
	
	if(!$plugin){
		exi('请先选择查找模块','error');
	}
	$dir=ROOT_D.'/addons/'.$plugin;
	//整理替换数组
	$search=$replace=array();
	$seh_rep=trim($post['seh_rep'],';');
	if(!empty($seh_rep)){
		$seh_rep=explode(';',$seh_rep);
		$replaces=$this->get_list('replace',array('params'=>array('id'=>$seh_rep)));
		foreach($replaces as $rep){
			$search[]=base64_decode($rep['search']);
			$replace[]=base64_decode($rep['replace']);
		}
	}
	if($post['search'] && $post['replace']){
		$search[]=str_replace(array('&#039;'),array("'"),$post['search']);
		$replace[]=str_replace(array('&#039;'),array("'"),$post['replace']);
	}
	if(empty($search)){
		exi('没有选择替换内容！','error');
	}
	//设置查找文件类型
	$type=array();
	if($post['type']){
		$temp=trim($post['type'],';');
		if($temp){
			$temp=explode(';',$temp);
			foreach($temp as $row){
				$type[]=$return['types'][$row];
			}
		}
	}
	$files=cfc('files')->file_tree($dir,array(),$type);
	foreach($files as $file){
		if(is_file($dir.$file)){
			$data=file_get_contents($dir.$file);
			$data=str_replace($search,$replace,$data);
			file_put_contents($dir.$file,$data);
		}
	}
	exi('替换成功！');
}


